package com.employeemanagementsystem.controller;

import com.employeemanagementsystem.dao.FeedbackDao;
import com.employeemanagementsystem.model.Feedback;
import com.employeemanagementsystem.model.User;

import jakarta.servlet.http.HttpSession;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/submit_feedback")
public class FeedbackSubmitServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user != null) {
            request.getRequestDispatcher("/WEB-INF/jsp/feedback_submit.jsp").forward(request, response);
        } else {
            response.sendRedirect("login.jsp?error=请先登录");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("login.jsp?error=请先登录");
            return;
        }

        String content = request.getParameter("content");

        Feedback feedback = new Feedback();
        feedback.setEmployeeId(user.getId());
        feedback.setContent(content);
        feedback.setStatus("PENDING");

        FeedbackDao feedbackDao = new FeedbackDao();

        if (feedbackDao.addFeedback(feedback)) {
            // 使用 PRG 模式，重定向到员工主页
            response.sendRedirect("employee_home?message=反馈提交成功！");
        } else {
            response.sendRedirect("submit_feedback?error=反馈提交失败，请重试。");
        }
    }
}
